<?php
/**
 * Created by allen.qiang.
 * BD双11送礼包活动ajax
 * Date: 14-11-6
 * Time: 上午9:35
 * @author allen.qiang <allen.qiang@fistforward.com>
 * FUN-AC-0013
 */
quanli_require('module/func/eai.func.php');
quanli_require('module/base/activity/activitySpike.class.php');

class ajax_gifts{

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1://check商品 和 库存
                    $this->check();
                    break;
                case 2://用户领取礼包
                    $this->gotopack();
                    break;


            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    function check(){

        global $G_SHOP;
        //载入活动类
        quanli_require('module/base/activity/activitySpike.class.php');
        //实例化活动类
        $activityspike=new activitySpike();
        //当天开始日期
        $beginTime=date('Y-m-d').' 00:00:00';
        //当天结束日期
        $endTime=date('Y-m-d').' 23:59:59';
        //查询当天礼包剩余数量
        $activitie="select extend_1 from activities_extend where classifyID=? and createTime > ? and  createTime < ?";
        $activitie_num=$G_SHOP->DBCA->stmt_query($activitie,'sss',11001,$beginTime,$endTime);
        //查询当日库存上限 （库存上限减去已领取=还可领取的礼包数量）
        //查询已领取礼包数量
        $activitie_sql="select count(1) as useNum from bill_promotion_log where promotionID=? and createTime > ? and  createTime < ?";
        $activitie_query=$G_SHOP->DBCA->stmt_query($activitie_sql,'sss',$activityspike->paymentID_11_pc,$beginTime,$endTime);
        $num=$activitie_num[0]->extend_1-$activitie_query[0]->useNum;
        if($num<1){
            returnJsonData("对不起。已售罄", 400);
        }else{
            returnJsonData($_REQUEST['post_id'], 200);
        }

//        if($isFivePower==1){
//            returnJsonData($_REQUEST['post_id'], 200);
//        }else{
//            returnJsonData('不具备五折购资格', 400);
//        }
    }

    function gotopack(){
        global $G_SHOP;
        //载入活动类
        quanli_require('module/base/activity/activitySpike.class.php');
        //实例化活动类
        $activityspike=new activitySpike();
        //获取客户号
        $pcustID = $G_SHOP->currentPCustID;

        if(!empty( $_REQUEST['sex'])){
            $sex=$_REQUEST['sex']=='boy' ? $activityspike->paymentID_11_man : $activityspike->paymentID_11_women;
        }else{
            $sex=$activityspike->paymentID_11_pet;
        }
        //查询当天礼包剩余数量
        $activitie="select * from activities_extend where extend_8=? order by createTime desc";
        $activitie_query=$G_SHOP->DBCA->stmt_query($activitie,'s',$pcustID);
        foreach($activitie_query as  $activitie_array){
            if(empty($activitie_array->extend_4) and empty($activitie_array->extend_5)){
                $userpack[]=array(
                    'bill_goods_no'=>$activitie_array->extend_9,
                    'bill_code'=>$activitie_array->extend_1
                );
            }
        }
        $activities_extend=new entity("activities_extend");
        $activities_extend->extend_9=$userpack[0]['bill_goods_no'];
        $activities_extend->extend_1=$userpack[0]['bill_code'];
        $G_SHOP->DBCA->fetch($activities_extend);
        $activities_extend->extend_4= $_REQUEST['name'];
        $activities_extend->extend_5= $_REQUEST['mobile'];
        $activities_extend->extend_6=$sex;
        $G_SHOP->DBCA->save($activities_extend);
        returnJsonData("OK", 200);
    }































}